<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
use app\api\model\PrepareModel;
use app\api\validate\PrepareValidate;
use app\http\TokenTrait;
class Prepare extends Common
{
    use TokenTrait;
    /**
     * @return \think\response\Json
     */
    public function PrepareList(Request $request,PrepareModel $model)
    {
        $data =$request->param();
        $data['start_time'] =isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] =isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $list = $model->getPrepareList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 配货单 新增
     * @return \think\response\Json
     */
    public function PrepareAdd(Request $request,PrepareModel $model)
    {
        $data =$request->param();
        $data['number'] = 'jxc'.time();
        $data['create_time'] =strtotime($data['create_time']);
        $data['maker'] =self::getUId();
        $data['type'] = 2;
        $data['images'] =json_encode($data['images']);
        if($data['mode'] != '自提'){
            $data['mode_address'] =json_encode($data['mode_address']);
        }
        $arr =$model->getPrepareAdd($data);
        if(!empty($arr)){
            return json(['code'=>200,'msg'=>'成功']);
        }else{
            return json(['code'=>500,'msg'=>'失败']);
        }
    }

    public function PrepareDraft($id,Request $request,PrepareModel $model)
    {
        $data =$request->post();
        $data['maker'] = self::getUId();//制单人id
        $data['create_time'] =strtotime($data['create_time']);
        $data['type'] =2;//2配货单
        $data['status'] =3;
        $data['images'] =json_encode($data['images']);
        if($data['mode'] != '自提'){
            $data['mode_address'] =json_encode($data['mode_address']);
        }
        $list = $model->getPrepareDraft($id,$data);
        if (is_int($list)) {
            return $this->returnJson(200,'ok',$list);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function getPutPrepare($id,Request $request,PrepareModel $model)
    {
        try{
            $data =$request->post();
            $data['maker'] = self::getUId();//制单人id
            $data['create_time'] =strtotime($data['create_time']);
            $data['type'] =2;//2配货单
            $data['status'] =5;
            $data['images'] =json_encode($data['images']);
            if($data['mode'] != '自提'){
                $data['mode_address'] =json_encode($data['mode_address']);
            }
            $arr = $model->getPutStock($id,$data);
            if(is_string($arr)){
                return $this->returnJson(200,  'ok');
            }else{
                return $this->returnJson(500,  'fail');
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    /**
     * 配货订单  ------详情
     * @param $id 配货订单id
     * @return \think\response\Json
     */
    public function PrepareDetail($id,PrepareModel $model)
    {
        $list = $model->getPrepareInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 配货订单  ------留言
     * @param $id 配货订单id
     * @return \think\response\Json
     */
    public function message(Request $request,PrepareModel $model)
    {
        $data = $request->param();
        $data['create_time'] = time();
        $list = $model->addMessage($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 配货订单  ------留言删除
     * @param $id 留言id
     * @return \think\response\Json
     */
    public function delMessage($id,PrepareModel $model)
    {
        $list = $model->getDelMessage($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 配货订单  ------删除
     * @param $id 配货订单id
     * @return \think\response\Json
     */
    public function PrepareDel($id,PrepareModel $model)
    {
        try {
            $list = $model->getPrepareDel($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    /**
     * 配货订单  ------撤销
     * @param $id 配货订单id
     * @return \think\response\Json
     */
    public function PrepareRevoke($id,PrepareModel $model)
    {
        $list = $model->getPrepareRevoke($id);
        if ($list ==2) {
            return json(['code' => 500, 'msg' => '无法撤销,请先操作对应出库单']);
        } else {
            return json(['code' => 200, 'msg' => '成功']);
        }
    }

    /**
     * 配货订单 部分出库
     */
    public function depot($id,Request $request,PrepareModel $model)
    {
        $option = json_decode($request->param('option'),true);
        try {
            $list = $model->getDepot($id, $option);
            if ($list == 1) {
                return json(['code' => 200, 'msg' => '成功']);
            } else {
                return json(['code' => 500, 'msg' => '输入数量超出库存上限']);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    /**
     * 配货订单 全部出库
     * @return string|\think\response\Json
     */
    public function alldepot($id,PrepareModel $model)
    {
        $list = $model->getAllDepot($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 配货退货  列表
     * @return string|\think\response\Json
     */
    public function returnList(Request $request,PrepareModel $model)
    {
        $data =$request->param();
        $data['start_time'] =isset($data['start_time']) ? strtotime($data['start_time']) : '';
        $data['end_time'] =isset($data['end_time']) ? strtotime($data['end_time']) : '';
        $list = $model->getReturnList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 配货退货  新增
     * @return \think\response\Json
     */
    public function returnAdd(Request $request,PrepareModel $model)
    {
        $data =$request->param();
        $data['number'] = 'jxc' . time();
        $data['create_time'] = strtotime($data['create_time']);
        $data['type'] =5;
        $data['maker'] =self::getUId();
        $data['images'] =json_encode($data['images']);
        $list = $model->getReturnAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function returnDraft($id,Request $request,PrepareModel $model)
    {
        $data =$request->post();
        $data['number'] = 'jxc' . time();//单号
        $data['uid'] = self::getUId();//制单人id
        $data['create_time'] =strtotime($data['create_time']);
        $data['type'] =5;//5配货退货单
        $data['status'] =3;
        $data['images'] =json_encode($data['images']);
        $list = $model->returnDraft($id,$data);
        if (is_int($list)) {
            return $this->returnJson(200,'ok',$list);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function prepareReturn($id,Request $request,PrepareModel $model)
    {
        $data =$request->post();
        $data['maker'] = self::getUId();//制单人id
        $data['create_time'] =strtotime($data['create_time']);
        $data['type'] =5;//5配货退货单
        $data['status'] =1;
        $data['images'] =json_encode($data['images']);
        $list = $model->prepareReturn($id,$data);
        if (is_int($list)) {
            return $this->returnJson(200,'ok');
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 配货退货  ------详情
     * @param $id 配货退货id
     * @return \think\response\Json
     */
    public function returnDetail($id,PrepareModel $model)
    {
        $list = $model->getDetail($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function returnDepot($id,Request $request,PrepareModel $model)
    {
        $option =json_decode($request->param('option'),true);
        try {
            $list = $model->getReturnDepot($id, $option);
            if ($list == 1) {
                return json(['code' => 200, 'msg' => '成功']);
            } else {
                return json(['code' => 500, 'msg' => '输入数量超出库存上限']);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    /**
     * 配货退货  ------撤销
     * @param $id 配货退货单id
     * @return \think\response\Json
     */
    public function returnRevoke($id,PrepareModel $model)
    {
        try {
            $list = $model->getReturnRevoke($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    /**
     * 配货退货  ------删除
     * @param $id 配货退货单id
     * @return \think\response\Json
     */
    public function returnDel($id,PrepareModel $model)
    {
        try {
            $list = $model->getReturnDel($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }
}